<?php

if_get('/address_slots/add', function ()
{
    $estate = input_entity('estate');

    return render('address_slot/add', [
        'estate' => $estate,
    ]);
});

if_post('/address_slots/add', function ()
{
    $address_slot = address_slot::create();

    $address_slot->rule = input('rule');
    $address_slot->number = input('number');
    $address_slot->estate_id = input('estate_id');

    return redirect('/estates/detail/'.$address_slot->estate_id);
});

if_get('/address_slots/detail/*', function ($address_slot_id)
{
    $address_slot = dao('address_slot')->find($address_slot_id);
    otherwise($address_slot->is_not_null(), 'address_slot not found');

    return render('address_slot/detail', [
        'address_slot' => $address_slot,
    ]);
});

if_get('/address_slots/update/*', function ($address_slot_id)
{
    $address_slot = dao('address_slot')->find($address_slot_id);
    otherwise($address_slot->is_not_null(), 'address_slot not found');

    return render('address_slot/update', [
        'address_slot' => $address_slot,
    ]);
});

if_post('/address_slots/update/*', function ($address_slot_id)
{
    $address_slot = dao('address_slot')->find($address_slot_id);
    otherwise($address_slot->is_not_null(), 'address_slot not found');

    $address_slot->rule = input('rule');
    $address_slot->number = input('number');

    return redirect('/estates/detail/'.$address_slot->estate_id);
});

if_post('/address_slots/delete/*', function ($address_slot_id)
{
    $address_slot = dao('address_slot')->find($address_slot_id);
    otherwise($address_slot->is_not_null(), 'address_slot not found');

    $address_slot->delete_relation_entities();
    $address_slot->delete();

    return redirect('/estates/detail/'.$address_slot->estate_id);
});
